package com.example.lenovo.demo.presenter.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baidu.platform.comapi.map.p;
import com.example.lenovo.demo.R;
import com.example.lenovo.demo.model.databean.StrategyBean;

import java.util.List;

/**
 * Created by Lenovo on 2017/1/15.
 */

public class StrategyChildRvAdapter extends RecyclerView.Adapter<StrategyChildRvAdapter.ViewHolder> {
    private List<StrategyBean.PagesBean.ChildrenBean> data;
    private Context context;

    public StrategyChildRvAdapter(List<StrategyBean.PagesBean.ChildrenBean> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public StrategyChildRvAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.strategy_child_rvitem,parent,false));

    }

    @Override
    public void onBindViewHolder(final StrategyChildRvAdapter.ViewHolder holder, int position) {
        StrategyBean.PagesBean.ChildrenBean childrenBean=data.get(position);
        holder.textView.setText(childrenBean.getTitle());
        StrategyChildFg_Rv_item_RvAdapter adapter=new StrategyChildFg_Rv_item_RvAdapter(childrenBean.getSections(),context);
        LinearLayoutManager manager=new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false);
        manager.setAutoMeasureEnabled(true);
        holder.recyclerView.setLayoutManager(manager);
        holder.recyclerView.setAdapter(adapter);
        holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if(holder.recyclerView.getVisibility()==View.VISIBLE){
                    holder.recyclerView.setVisibility(View.GONE);
                    holder.imageView.setImageResource(R.drawable.down);
                }else {
                    holder.recyclerView.setVisibility(View.VISIBLE);
                    holder.imageView.setImageResource(R.drawable.up);
                }


            }
        });
    }

    @Override
    public int getItemCount() {
        return data==null?0:data.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout linearLayout;
        TextView textView;
        ImageView imageView;
        RecyclerView recyclerView;
        public ViewHolder(View itemView) {
            super(itemView);
            linearLayout= (LinearLayout) itemView.findViewById(R.id.strategy_child_rvitem_li1);
            recyclerView= (RecyclerView) itemView.findViewById(R.id.strategy_child_rvitem_rv);
            textView= (TextView) itemView.findViewById(R.id.strategy_child_rvitem_tv);
            imageView= (ImageView) itemView.findViewById(R.id.strategy_child_rvitem_img);
        }
    }
}
